package com.openstackapi.ctds.imp;

import java.util.ArrayList;

import java.util.List;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.model.storage.block.VolumeType;
import org.openstack4j.openstack.OSFactory;

import com.openstackapi.ctds.FileStorageController;
import com.openstackapi.ctds.pojo.AclIP;
import com.openstackapi.ctds.pojo.CreatFileStorageResp;
import com.openstackapi.ctds.pojo.DeleteFileStorageResp;
import com.openstackapi.ctds.pojo.FileStorageParamArray;
import com.openstackapi.ctds.pojo.SetFileStorageAccessResp;
import com.openstackapi.ctds.pojo.UpdateFileSorageSizeResp;

public class FileStorageControllerImp implements FileStorageController {

	
	//create file storage
	@Override
	public CreatFileStorageResp CreatFileStorage(String fileStorageTemplateId,
			FileStorageParamArray fileStorageParamArray, String fileStorageName, String appID, String appName) {


		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		
		Volume v = os.blockStorage().volumes()
				     	.create(Builders.volume()
				        .name("baater111 Volume")
				        .volumeType("cephtype")  
				        .description("baater volume created from java code")
				        .size(1)    //单位为 GB
				        .build()
				       );

		System.out.println(v); 	
		/**
		* 返回值为
		* CinderVolume{id=034ec4db-edb6-40ef-beff-4d8dac0dd129, name=baater Volume, 
		* description=baater volume created from java code, status=creating, size=2, 
		* zone=nova, created=Mon Nov 28 09:04:49 CST 2016, metadata={}, bootable=false}
		*/
		
		
		
		
		return null;
	}
	
	
	//delete file storage
	@Override
	public DeleteFileStorageResp DeleteFileStorage(String FileStorageID) {
		
		
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		// delete specific volume by id
		ActionResponse ar1 = os.blockStorage().volumes().delete("bf2aea50-1042-4707-a5c7-f067dac703d2");
		
		return null;
	}

	
	
	
	//update file storage
	@Override
	public UpdateFileSorageSizeResp UpdateFileSorageSize(String fileStorageID, int quotaSize) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		//update the quota for specific volume by id
		ActionResponse ar = os.blockStorage().volumes().extend("99607660-9d63-4bae-b591-e72f27b9b493", 2);
		System.out.println(ar);
		System.out.println(ar.getFault());    // error report
		System.out.println(ar.getCode());   // Success for 200     failure for 
		
		
		return null;
	}




	@Override
	public com.openstackapi.ctds.pojo.QueryFileStorageResp QueryFileStorage(
			List<com.openstackapi.ctds.pojo.FileStorageID> FileStorageIDs) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public SetFileStorageAccessResp SetFileStorageAccess(String FileStorageID, List<AclIP> FileRequestAcl) {

		//login
		OSClientV2 os = OSFactory.builderV2()
		                .endpoint("http://10.50.0.10:5000/v2.0")
		            .credentials("admin","ADMIN_PASS")
		            .tenantName("admin")
		            .authenticate();
		System.out.println("authentication complate");
		
		
		
		// list volume type
		List<? extends VolumeType> types = os.blockStorage().volumes().listVolumeTypes();
		
		
		
		return null;
	}


	
	
	//test 
	public static void main(String[] args){
		
		
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.10:5000/v2.0")
	                       .credentials("admin","ADMIN_PASS")
	                       .tenantName("admin")
	                       .authenticate();
		System.out.println("authentication complate");
		
		
//		// list volume type
//		List<? extends VolumeType> types = os.blockStorage().volumes().listVolumeTypes();
//		for(VolumeType t:types){
//			System.out.println("volume type = " + t.getName());
//			System.out.println("volume type body = " + t);
//		}
//		
//		System.out.println("xxxxxxxxxxxxx");
//		
//		
		// list all volumes
		List<? extends Volume> Volume = os.blockStorage().volumes().list();
		for(Volume t:Volume){
			System.out.println("volume list = " + t);
			
		}

		// list specific volume by id
		Volume volume = os.blockStorage().volumes().get("99607660-9d63-4bae-b591-e72f27b9b493");
		System.out.println(volume);
		/**
		 * CinderVolume{id=99607660-9d63-4bae-b591-e72f27b9b493, 
		 * name=baater222 Volume, description=baater volume created from java code, 
		 * status=available, size=3, zone=nova, created=Mon Nov 28 10:10:51 CST 2016, 
		 * volumeType=cephtype, metadata={readonly=False}, bootable=false}
		 */
		
		
		//Simple Tenant Usage (os-simple-tenant-usage)
		
//		List<? extends SimpleTenantUsage> tenantUsages = os.compute().quotaSets().listTenantUsages();
//		System.out.println(tenantUsages);
		
		/**
		 *  NovaSimpleTenantUsage{tenantId=0de3bafeabb548e6b1003c3b7125729f, 
		 *  totalMemoryMbUsage=0.00004835555555555556, 
		 *  totalVcpusUsage=4.7222222222222226E-8, 
		 *  totalLocalGbUsage=4.7222222222222226E-7, 
		 *  start=Mon Nov 28 17:28:40 CST 2016, stop=Mon Nov 28 17:28:40 CST 2016, 
		 *  totalHours=4.7222222222222226e-08, 
		 */
	
		
		

		
		
//		//detach volume
//		ServerService ss1 = new ServerServiceImpl();
//		                       //detachVolume(String serverId, String attachmentId)   attachmentId = volumeID
//		ActionResponse ar = ss1.detachVolume("f8d4a12e-2380-4a6b-85f1-01971a2f010e", "99607660-9d63-4bae-b591-e72f27b9b493");
////		System.out.println(ar);
//		if(!ar.isSuccess()){
//			System.out.println("ERROR : " + ar.getFault());
//		}
		
		
		
		
		
	}



	

}
